*******************
***** Table 5 *****
*******************
global Section4 "D:\Dropbox\unequal_gains\QJE revision plan\analysis\section4_data"
global resultspath "D:\Dropbox\unequal_gains\QJE revision plan\analysis\clean_results"

use "$Section4/analysis_main.dta", clear

* Set up table
gen Description=" "
replace Description="Beta" if [_n]==1
replace Description="SE" if [_n]==2
replace Description="First Stage F" if [_n]==3
* note first stage F stat will be adjusted for clustering across product modules
replace Description="Cragg-Donald F" if [_n]==4
replace Description="N" if [_n]==5

* Col (1) first stage
reg RMS_avg_Dlog_realspending inst_linageres [aw=sqrt(RMS_avg_spending)], cluster(product_module_code)
gen Col_1=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_1=`b' if Description=="Beta"
replace Col_1=`se' if Description=="SE"
replace Col_1=e(N) if Description=="N"

* Col (2) OLS continued prod. infl. reduced form
reg RMS_lces_price_index_win inst_linageres [aw=sqrt(RMS_avg_spending)], cluster(product_module_code)
gen Col_2=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_2=`b' if Description=="Beta"
replace Col_2=`se' if Description=="SE"
replace Col_2=e(N) if Description=="N"

* Col (3) IV continued prod. infl
ivreg2 RMS_lces_price_index_win (RMS_avg_Dlog_realspending=inst_linageres) [aw=sqrt(RMS_avg_spending)] ///
, first cluster(product_module_code)
gen Col_3=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_3=`b' if Description=="Beta"
replace Col_3=`se' if Description=="SE"
replace Col_3=e(rkf) if Description=="First Stage F"
replace Col_3=e(cdf) if Description=="Cragg-Donald F"
replace Col_3=e(N) if Description=="N"

* Col (4) IV continued prod. infl --- full residualization of instrument
ivreg2 RMS_lces_price_index_win (RMS_avg_Dlog_realspending=inst_allres) [aw=sqrt(RMS_avg_spending)], first ///
cluster(product_module_code)
gen Col_4=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_4=`b' if Description=="Beta"
replace Col_4=`se' if Description=="SE"
replace Col_4=e(rkf) if Description=="First Stage F"
replace Col_4=e(cdf) if Description=="Cragg-Donald F"
replace Col_4=e(N) if Description=="N"

* Col (5) IV continued prod. infl --- full residualization of instrument & decile-group f.e.
ivreg2 RMS_lces_price_index_win (RMS_avg_Dlog_realspending=inst_allres) i.product_group_code i.quality_rank ///
[aw=sqrt(RMS_avg_spending)], partial(i.product_group_code i.quality_rank) first ///
cluster(product_module_code)
gen Col_5=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_5=`b' if Description=="Beta"
replace Col_5=`se' if Description=="SE"
replace Col_5=e(rkf) if Description=="First Stage F"
replace Col_5=e(cdf) if Description=="Cragg-Donald F"
replace Col_5=e(N) if Description=="N"

* Col (6) IV continued prod. infl --- clustered SE, same point estimate as in 5
** use R code **

* Col (7) OLS full infl. reduced form
reg RMS_lfullces_pi_win_common inst_linageres [aw=sqrt(RMS_avg_spending)], cluster(product_module_code)
gen Col_7=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_7=`b' if Description=="Beta"
replace Col_7=`se' if Description=="SE"
replace Col_7=e(N) if Description=="N"

* Col (8) IV full infl
ivreg2 RMS_lfullces_pi_win_common (RMS_avg_Dlog_realspending=inst_linageres) [aw=sqrt(RMS_avg_spending)] ///
, first cluster(product_module_code)
gen Col_8=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_8=`b' if Description=="Beta"
replace Col_8=`se' if Description=="SE"
replace Col_8=e(rkf) if Description=="First Stage F"
replace Col_8=e(cdf) if Description=="Cragg-Donald F"
replace Col_8=e(N) if Description=="N"

* Col (9) IV full infl ---  full residualization of instrument
ivreg2 RMS_lfullces_pi_win_common (RMS_avg_Dlog_realspending=inst_allres) [aw=sqrt(RMS_avg_spending)], first ///
cluster(product_module_code)
gen Col_9=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_9=`b' if Description=="Beta"
replace Col_9=`se' if Description=="SE"
replace Col_9=e(rkf) if Description=="First Stage F"
replace Col_9=e(cdf) if Description=="Cragg-Donald F"
replace Col_9=e(N) if Description=="N"

* Col (10) IV full infl --- full residualization of instrument & decile-group f.e.
ivreg2 RMS_lfullces_pi_win_common (RMS_avg_Dlog_realspending=inst_allres) i.product_group_code i.quality_rank ///
[aw=sqrt(RMS_avg_spending)], partial(i.product_group_code i.quality_rank) first cluster(product_module_code)
gen Col_10=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_10=`b' if Description=="Beta"
replace Col_10=`se' if Description=="SE"
replace Col_10=e(rkf) if Description=="First Stage F"
replace Col_10=e(cdf) if Description=="Cragg-Donald F"
replace Col_10=e(N) if Description=="N"

* Col (11) IV full infl --- clustered SE
** use R code **

* Finalize table and save
order Descr Col*
keep Descr Col*
format Col* %6.3f
drop if [_n]>5
save "$resultspath\Table5", replace
